package map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;



/**
 * Created by 佳星 on 08-08-0008.
 */
public class ImageMap extends FrameLayout implements PointExtension.OnPointActionListener{
    private CoordMap coordMap;
    private MapWidget  widget;

    public ImageMap(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public ImageMap(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ImageMap(Context context) {
        this(context, null);
    }

    /**
     * 初始化
     * @param context [description]
     */
    private void init(Context context){
        coordMap = new CoordMap(context);
        coordMap.setOnPointActionListener(this);
        int match = LayoutParams.MATCH_PARENT;
        LayoutParams params = new LayoutParams(match, match);
        addView(coordMap, params);

    }

    // /**
    //  * [setWidget description]
    //  * @param view           [description]
    //  * @param renderDelegate [description]
    //  */
    // public void setWidget(View view, MapWidget.RenderDelegate renderDelegate){
    //     if (view == null){
    //         return;
    //     }
    //     widget = new MapWidget(view);
    //     widget.setRenderDelegate(renderDelegate);
    //     addView(widget);
    //     widget.view.setVisibility(View.VISIBLE);
    // }

    /**
     * [setWidget description]
     * @param widget [description]
     */
    public void setWidget(MapWidget widget) {
        if (widget == null){
            return;
        }
        this.widget = widget;
        addView(widget);
        widget.view.setVisibility(View.VISIBLE);
    }

    // /**
    //  * [addPointAndRefToWidget description]
    //  * @param point [description]
    //  */
    // public void addPointAndRefToWidget(final MapPoint point){
    //     if(widget != null){
    //         point.setWidget(widget);
    //     }
    //     addPoint(point);
    // }

    /**
     * [添加带widget点]
     * @param point  [description]
     * @param widget [description]
     */
    public void addPointWithWidget(final MapPoint point, final MapWidget widget){
        this.setWidget(widget);
        point.setWidget(widget);
        addPoint(point);
    }

    /**
     * [加个点]
     * @param point [description]
     */
    public void addPoint(MapPoint point){
        float scale = coordMap.getScale();
        point.onScale(scale);

        // 将图像中心移动到目标形状的中心坐标上
        // Move the center point of the image to the target shape center.
        PointF from = coordMap.getCenter();
        PointF to = point.getCenter();

        PointF offset = coordMap.getAbsoluteCenter();
        point.onTranslate(offset.x, offset.y);
        coordMap.addPoint(point);
    }

    /**
     * [removePoint]
     * @param tag [description]
     */
    public void removePoint(Object tag){
        coordMap.removePoint(tag);
    }

    /**
     * [clearPoints]
     */
    public void clearPoints(){
        for(MapPoint item : coordMap.getPoints()) {
            item.cleanWidget();
        }
        coordMap.clearPoint();
        if (widget != null){
            widget.view.setVisibility(View.GONE);
        }
    }

    /**
     * [设置地图]
     * @param bitmap [description]
     */
    public void setBitmap(Bitmap bitmap){
        coordMap.setBitmap(bitmap);
    }

    /**
     * [onPointClick]
     * @param point    [description]
     * @param xOnImage [description]
     * @param yOnImage [description]
     */
    @Override
    public void onPointClick(MapPoint point, float xOnImage, float yOnImage) {
        Log.v("onPointClick","click");
    }
}
